package handlers

import (
	"code.google.com/p/jesus-1496/auth/sessionshelper"
	"code.google.com/p/jesus-1496/servererror"
	"code.google.com/p/jesus-1496/serverlog"
	"code.google.com/p/jesus-1496/utils/env"
	"net/http"
)

var logger = serverlog.NewServerLog("handlers")

//initial handler that is called before the user logs in.
func IndexHandler(w http.ResponseWriter, r *http.Request) *servererror.ServerError {
	u, err := sessionshelper.GetSessionUser(w, r)
	if u != nil {
		//User is logged in. Woop
		logger.Printf("User %s already logged in, using homeHandler.", u.Email)
		HomeHandler(w, r)
		return nil
	} else {
		logger.Printf("No user logged in, or error getting user. Error from GetSessionUser: %v", err)
	}
	t, _, err := env.NewHtmlTemplateFromFile("index.html")
	if err != nil {
		return servererror.NewServerError(err, "Internal server error.", http.StatusInternalServerError)
	} else {
		w.Header().Set("Content-Type", "text/html")
		t.Execute(w, nil)
	}
	return nil
}
